package com.py.gs.util.base.concurrent;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 有名字的线程生成器
 */
public class NamedThreadFactory implements ThreadFactory {

	private final AtomicInteger mThreadNum = new AtomicInteger(1);
	private String prefix = "";
	private final ThreadGroup mGroup;

	public NamedThreadFactory(String prefix) {
		SecurityManager s = System.getSecurityManager();
		this.mGroup = s == null ? Thread.currentThread().getThreadGroup() : s.getThreadGroup();
		this.prefix = prefix;
	}

	protected String getPrefix() {
		return prefix;
	}

	@Override
	public Thread newThread(Runnable runnable) {
		String name = this.getPrefix() + "-" + this.mThreadNum.getAndIncrement();
		Thread ret = new Thread(this.mGroup, runnable, name, 0L);
		return ret;
	}
}
